#!/bin/env python 
import os
import time
import commands
import argparse

def options():
  parser = argparse.ArgumentParser()
  parser.add_argument("-a", "--action", default="", help='action todo[start/kill/deploy/policy/impdl]')
  parser.add_argument("-s", "--script", default="handler.sh", help='script for exec')
  parser.add_argument("-u", "--user", default="root", help='login user')
  parser.add_argument("-p", "--passwd", default="passwd", help='login passwd')
  #parser.add_argument("--max_depth", default=5, type=int,help='maximum depth')
  parser.add_argument("--ip", default="iplist", help='ip list file')
  opt = parser.parse_args()
  return opt

def exec_cmd(cmd):
  status, output = commands.getstatusoutput(cmd)
  print "status:{} cmd:[{}]".format(status, cmd)

def check_dict(script, ip, action, user, pwd):
  cmd = "bash -x {} {} {} '{}' {}".format(script, ip, user, pwd, action)
  print cmd

  status, output = commands.getstatusoutput(cmd)
  print ip, "------", output.split("\n")


if __name__ == "__main__":
  opt = options()

  if (opt.action != ""):
    for line in open(opt.ip):
      check_dict(opt.script, line.strip(), opt.action, opt.user, opt.passwd)
      time.sleep(1)
